// pages/myMoment/myMoment.js

let app = getApp()

import req from '../../utils/req';
import utils from '../../utils/utils';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moment: [],
    strategy: [],
    totalMoment: 0,
    totalStrategy: 0,
    currentMomentPage: 1,
    currentStrategyPage: 1,
    switch: 'moment',
    uid: '',
    avatar: '',
    name: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pages = getCurrentPages(); //页面栈
    let beforePage = pages[pages.length - 2];
    let data = {}
    if (beforePage.route === "pages/me/me") {
      data = {
        uid: app.globalData.userInfo.uid,
        globalUid: app.globalData.userInfo.uid,
        avatar: app.globalData.userInfo.avatar,
        name: app.globalData.userInfo.name
      }
    } else {
      data = {
        uid: options.uid,
        avatar: options.avatar,
        name: options.name,
        globalUid: app.globalData.userInfo.uid,
      }
    }
    this.setData(data)
    this.getMoment()
    this.getStrategy()
  },

  getMoment(type) {
    const data = {
      uid: this.data.uid,
      page: this.data.currentMomentPage
    }
    type === 'refrash' ? wx.showNavigationBarLoading() : '' //刷新加载
    req.get('加载中', '/moment/getMoment', data, (res) => {
      const data = res.data
      data.list.forEach(e1 => {
        const images = []
        e1['images'].forEach(e2 => {
          images.push(e2.url)
        })
        e1.images = images
        e1['createTime'] = utils.formatTime(e1['createTime'])
      });
      this.setData({
        totalMoment: data.total,
        moment: this.data.moment.concat(data.list)
      })
      if (type === 'refrash') {
        wx.hideNavigationBarLoading(); //完成停止加载图标
        wx.stopPullDownRefresh();
        utils.toast('success', '刷新成功', 500)
      }
    })
  },
  getStrategy(type) {
    type === 'refrash' ? wx.showNavigationBarLoading() : '' //刷新加载
    const data = {
      page: this.data.currentStrategyPage,
      uid: this.data.uid
    }
    req.get('加载中', '/moment/getStrategy', data, (res) => {
      const data = res.data
      data.list.forEach(e1 => {
        const images = []
        let taps = e1['taps'].split(',')
        e1['images'].forEach(e2 => {
          images.push(e2.url)
        })
        e1.images = images
        e1.taps = taps
        e1['createTime'] = utils.timeFormat(e1['createTime'])
      });
      this.setData({
        totalStrategy: data.total,
        strategy: this.data.strategy.concat(data.list)
      })
      if (type === 'refrash') {
        wx.hideNavigationBarLoading(); //完成停止加载图标
        wx.stopPullDownRefresh();
        utils.toast('success', '刷新成功', 500)
      }
    })
  },
  del(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '确认删除?',
      success: function (res) {
        if (res.confirm) {
          const data = {
            mid: e.currentTarget.dataset.mid
          }
          req.post('删除中', '/moment/deleteMoment', data, (res) => {
            if (res.status === 200) {
              utils.toast('success', res.msg)
              const moment = that.data.moment
              moment.splice(e.currentTarget.dataset.idx, 1)
              that.setData({
                moment,
                totalMoment: that.data.totalMoment - 1
              })
            } else {
              utils.toast('error', res.msg)
            }
          })
        }
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.img, // 当前显示图片的http链接
      urls: e.currentTarget.dataset.image // 需要预览的图片http链接列表
    })
  },
  handleSwitch(e) {
    const sth = e.currentTarget.dataset.type
    this.setData({
      switch: sth
    })
  },
  goToDetail(e) {
    let url = '../detail/detail?sid=' + e.currentTarget.dataset.sid
    wx.navigateTo({
      url
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const type = 'refrash';
    this.setData({
      currentMomentPage: 1,
      currentStrategyPage: 1,
      strategy: [],
      moment: []
    })
    this.getMoment(type)
    this.getStrategy(type)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let status = this.data.switch === "moment"
    const lengthStatus = status ? (this.data.moment.length < this.data.totalMoment) : (this.data.strategy.length < this.data.totalStrategy)
    const pageTpye = status ? {
      currentMomentPage: this.data.currentMomentPage + 1
    } : {
      currentStrategyPage: this.data.currentStrategyPage + 1
    }
    if (lengthStatus) {
      this.setData(pageTpye)
      status ? this.getMoment("add") : this.getStrategy("add")
    } else {
      utils.toast('none', '到底啦')
    }

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})